package se.hiq.wineo.client.widgets;

import java.util.ArrayList;
import java.util.Arrays;

import se.hiq.wineo.shared.Tools;
import se.hiq.wineo.shared.data.WineDescriptor;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HTMLTable.Cell;

/**
 * Displays a list of available wines. Has one button for display currently selected
 * wine and another one for creating a new wine.
 * 
 * @author joakimsah
 *
 */
public class WineListWidget extends Composite {

	// Widgets in this Composite
	private Grid mainPanel;
	private Label wineListLabel;
	private Grid wineTable;
	private HorizontalPanel buttonPanel;
	private Button changeButton;
	private Button newButton;
	
	// Private data
	private ArrayList<WineDescriptor> wines = new ArrayList<WineDescriptor>();
	private Integer selectedRow = null;
	
	public WineListWidget() {
		mainPanel = new Grid(3, 1);
		mainPanel.setStyleName("wineListWidget");
		
		wineListLabel = new Label("Vinlista");
		wineListLabel.setStyleName("wineHeading");
		mainPanel.setWidget(0, 0, wineListLabel);
				
		updateWineList();
		
		buttonPanel = new HorizontalPanel();
		buttonPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		buttonPanel.setWidth("20em");
		newButton = new Button("Nytt vin");
		newButton.addClickHandler(newButtonClickHandler);
		buttonPanel.add(newButton);
		buttonPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		changeButton = new Button("Visa/ändra");
		changeButton.addClickHandler(changeButtonClickHandler);
		buttonPanel.add(changeButton);
		mainPanel.setWidget(2, 0, buttonPanel);	
		
		initWidget(mainPanel);
	}
	
	// ----- Public methods -----

	/**
	 * Update this widget with the latest list of available wines
	 * 
	 * @param winesArr The complete list of wines
	 */
	public void onCompleteWineList(WineDescriptor[] winesArr) {
		wines.clear();
		wines.addAll(Arrays.asList(winesArr));
		selectedRow = null;
		updateWineList();
	}
	
	/**
	 * Retrieve primary key of selected Wine.
	 * 
	 * @return PrimaryKey or null if no wine selected
	 */
	public Long getSelectedWinePrimaryKey() {
		if (selectedRow != null) {
			return wines.get(selectedRow - 1).getPrimaryKey();
		} else {
			return null;
		}
	}
	
	// ----- Private methods -----
	
	private void updateWineList() {
		if (wineTable != null) {
			mainPanel.remove(wineTable);
		}
		
		// Setup table and its heading and styles
		wineTable = new Grid(1 + wines.size(), 3);
		wineTable.setText(0, 0, "Namn");
		wineTable.setText(0, 1, "Färg");
		wineTable.setText(0, 2, "År");
		wineTable.addClickHandler(wineTableClickHandler);
		wineTable.setCellSpacing(0);
		wineTable.setCellPadding(2);
		wineTable.setWidth("20em");
		wineTable.getRowFormatter().setStyleName(0, "table-row-heading");
		wineTable.setStyleName("table-wines");
		
		// Add wines to table
		int row = 1;
		for (WineDescriptor w : wines) {
			wineTable.setText(row, 0, Tools.toStringOrEmpty(w.getName()));
			wineTable.setText(row, 1, Tools.toStringOrEmpty(w.getColor()));
			wineTable.setText(row, 2, Tools.toStringOrEmpty(w.getYear()));
			row++;
		}
		
		mainPanel.setWidget(1, 0, wineTable);
	}
	
	// ----- Classes for handling user input -----
	
	private ClickHandler wineTableClickHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			Cell cell = wineTable.getCellForEvent(event);
			if (selectedRow != null) {
				wineTable.getRowFormatter().setStyleName(selectedRow, "table-row-normal");
			}
			if (cell != null && cell.getRowIndex() > 0) {
				selectedRow = cell.getRowIndex();
				wineTable.getRowFormatter().setStyleName(selectedRow, "table-row-selected");
			} else {
				selectedRow = null;
			}
		}
	};
	
	private ClickHandler newButtonClickHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			// TODO Implementation goes here!
			Window.alert("newButtonClickHandler not implemented yet!");
		}
	};
	
	private ClickHandler changeButtonClickHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			// TODO Implementation goes here!
			Window.alert("changeButtonClickHandler not implemented yet!");
		}
	};
	
}
